package com.bocom.Intercepter;

import com.bocom.config.SpirngMvcConfig;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Author shenzhifeng
 * Create time  2019/9/23 10:22
 * Description
 * Modifier
 * Reasons
 */
public class SecurityInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
        System.out.println(requestUrl);
        response.setContentType("text/html;charset=utf-8");
        if (requestUrl.contains("wel/login.go") || requestUrl.contains("wel/loginVerify")) {
            return true;
        }

        HttpSession session = request.getSession();
        System.out.println(session.getId() +">>"+session.getAttribute(SpirngMvcConfig.SESSION_KEY));
//      判断是否已有该用户登录的session
        if (session.getAttribute(SpirngMvcConfig.SESSION_KEY) != null) {
            return true;
        }

//      跳转到登录页
        String url = "/webCache/wel/login.go";
        response.sendRedirect(url);
        return false;
    }

}
